import json

import requests
from lxml import etree
import re
import execjs

headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Pragma": "no-cache",
    "Sec-Fetch-Dest": "document",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-User": "?1",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
}


def get_token():
    url = "https://we.51job.com/api/job/search-pc"
    response = requests.get(url, headers=headers)
    response.encoding = response.apparent_encoding
    # print(response.text)
    # print(response)
    html = etree.HTML(response.text)
    script = html.xpath('/html/script/text()')[0].strip()
    token = re.findall("token: (.*?)?,", script, re.S)[0]
    print(token)
    refer = re.findall("refer: (.*?)?,", script, re.S)[0]
    print(refer)
    return token, refer


def check(token):
    js = execjs.compile(open('阿里滑块.js', encoding='utf-8').read())
    fy_token = js.call("get_fy_token")
    print(fy_token)
    # fy_token = '227!SSiSphGsE1Kq6IeHcU3vADudZ+P4JL9j4lPuOibZCSQWntR3SCcTpsrUVgiE9O3aPEMwCSPE8mJMtS3Oe8xcJPWY6+o1wHRenHEueEjypCASntkk+VnL3SIb++TfrYvCngjUOsvHaU3YJtmIHwonNhCkFnlHmzMinHEVOmv3ehuoV9CKrVn13DmWqAgHmppinHPkqfvHaRbgh70Ke8lnODlzqKiHDppinXPPOfHhJ5DeBoR8mxnD3Xlz1QVXmpxESEklOyJma+3Snz9kljnD3iS3qKoHSZvCnHEuOfvHaR3SHtR8lw6IdSS10QgHmppGcrCuOfvHaRMR3SRx5kfCy0v1N3yh7YU6/cehCgCYgJrDznPh5f9iy3WlzAyxgVKoJnuDSj33gJO6dDg+q1ZmfDjH+cNbMhWURI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990Z2vpX8A3ooWaZyI//rwAp67VUJC40lLvYlWiAlTUog0ckAVO1jmqAZlDAG7x5C'
    # uid_token = js.call("get_uid_token")
    uid_token = 'G0593B28820B0D2F00C736132E301BE9F632EB33EC34895D21D'
    # print(uid_token)
    p = {
        "key1": "code100",
        "user": "default",
        "ncbtn": "740.3333740234375|293.10418701171875|48|48|293.10418701171875|341.10418701171875|740.3333740234375|788.3333740234375",
        "umidToken": uid_token,
        "ncSessionID": "5e70deada55e",
        "et": "1"
    }
    url = "https://cf-app-waf.cfc.aliyuncs.com/nocaptcha/analyze.jsonp"
    params = {
        "a": "CF_APP_WAF",
        "t": token,
        "n": fy_token,
        "p": json.dumps(p, ensure_ascii=False, separators=(",", ":")),
        "scene": "register",
        "asyn": "0",
        "lang": "cn",
        "v": "1",
        "callback": "jsonp_0042746456711417524"
    }
    response = requests.get(url, headers=headers, params=params)

    print(response)
    res = json.loads(response.text.split('(')[1].split(')')[0])
    print(res)
    return res['result']['csessionid'], res['result']['value']


def get_data(token, session_id, sign, refer):
    url = "https://we.51job.com/api/job/search-pc"
    params = {
        "u_atoken": token,
        "u_asession": session_id,
        "u_asig": sign,
        "u_aref": refer
    }
    response = requests.get(url, headers=headers, params=params)

    print(response.text)
    print(response)


def main():
    token, refer = get_token()
    # token = '59c6953d-73a7-4de3-83f6-ea1d17870003'
    # refer = '3Txgx+McrUPhWvuKsYIVwJejs4A='
    session_id, sign = check(token)
    get_data(token, session_id, sign, refer)


if __name__ == '__main__':
    main()
